package iconeLibrary.view;

import iconeLibrary.model.Connection;
import iconeLibrary.controler.Controler;
import iconeLibrary.controler.ErrorsType;
import iconeLibrary.model.XQueryRequest;
import iconeLibrary.controler.TypeImage;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashSet;
import java.util.Set;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XQueryService;

/**
 * A simplistic security filter for Bugzooky that ensures that the user is logged in
 * before allowing access to any secured pages.
 *
 * @author Ayede Sonia :: Gaudino Anthony :: Herche Bilal :: Teixeira Tiago :: Touati Samed  :: Wadjahdi Marc
 */
public class IndexFilter implements Filter {

    private static Set<String> publicUrls = new HashSet<String>();


    static {
        publicUrls.add("/index.jsp");
    }

    /** Does nothing.
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    /** 
     * Method that apply filter .
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try {
            
            Controler controler = new Controler();
            controler.connection();
            XQueryRequest xQueryRequest = controler.getConnection().getXQueryRequest();
            String iconesNames = xQueryRequest.executeRequeteXQuery(Controler.queryIconesNamesFilePath, null);
            request.getSession().setAttribute("iconesNames", iconesNames);
            String filtresNames = xQueryRequest.executeRequeteXQuery(Controler.queryFiltersNamesFilePath, null);
            request.getSession().setAttribute("filtresNames", filtresNames);
            controler.getConnection().closeConnection();
            String typeImages = "";
            for(TypeImage type : TypeImage.values()){
                typeImages += "<option value=\"" + type.toString() + "\">" + type.toString() + "</option>\n";
            }
            request.getSession().setAttribute("typeImages", typeImages);
            filterChain.doFilter(request, response);
        } catch (XMLDBException ex) {

            request.getSession().setAttribute("errorName", ErrorsType.ErrorBD);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("ErrorServlet");
            requestDispatcher.forward(request, response);
        }

    }

    /**
     * Method that checks the request to see if it is for a publicly accessible resource
     * @param request 
     * @return
     */
    protected boolean isPublicResource(HttpServletRequest request) {
        String resource = request.getServletPath();

        return publicUrls.contains(request.getServletPath()) || (!resource.endsWith(".jsp") && !resource.endsWith(".action"));
    }

    /** Does nothing. */
    public void destroy() {
    }
}
